home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / graphs / displa~1.jav < prev    next >
Text File  |  1995-10-31  |  5KB  |  199 lines

  1. /* 
  2.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29.  
  30. import java.lang.*; 
  31. import java.io.InputStream;
  32. import java.awt.*;
  33. import java.net.*;
  34. import java.util.*;
  35.  
  36. /**
  37.  * A image loop applet, first raise smiles and then drop them
  38.  *
  39.  * @author      Siebe R. Brouwer
  40.  * version      1.1, 29 September 1995
  41.  */
  42.  
  43. // class Display, display prints the empty diagram to the screen 
  44. class Display extends Panel {
  45.     
  46.     protected String header;
  47.     protected String yText;
  48.     protected String xText;
  49.     protected int xTop;
  50.     protected int yTop;
  51.     protected int xGap;
  52.     protected int yGap;
  53.     protected int xStart;
  54.     protected int yStart;
  55.     protected Diagram diagram;
  56.     protected int moved;
  57.     protected Scrollbar scrollbar;
  58.  
  59.     //constructor
  60.     Display(String h, String xT, String yT, int xC,int yC) {
  61.     header = h;
  62.     xStart = 50;
  63.     xGap= xC;
  64.     yGap= yC;
  65.     yText = yT;
  66.     xText = xT;
  67.         moved = 0;
  68.         setLayout(new BorderLayout());
  69.     scrollbar = new Scrollbar(0);
  70.     scrollbar.setValues(0, 50,-1000,1000);
  71.     add("South",scrollbar);
  72.     }
  73.     
  74.     // draw a new diagram
  75.     public void drawDiagram(Diagram d) {
  76.     diagram = d;
  77.     repaint();
  78.     }
  79.     
  80.     // overwrite the paint method
  81.     public void paint(Graphics g) {
  82.     moved = scrollbar.getValue() * -1;
  83.     int gaps = getCaps(moved);
  84.     int left = getLeft(gaps, moved);
  85.     yStart = yStart();
  86.     xTop = xTop();
  87.     yTop = yTop();
  88.     scrollbar.reshape(xStart, yStart +40, xTop - xStart, 15);
  89.     drawAxiss(g, left);
  90.     g.drawString(header, xStart, 40);
  91.     g.drawString(xText, xTop - 100, yStart +30);
  92.     g.drawString(yText, 10, yTop - 15);
  93.     diagram.drawData(g, gaps, left, moved);
  94.     }    
  95.     
  96.     /* draw the x-axis and y-axis to the screen
  97.      * including the markers on the axis.
  98.      */
  99.        protected void drawAxiss(Graphics g, int left) {
  100.     yStart = yStart();
  101.      g.drawLine(xStart, yStart, xTop, yStart);
  102.     g.drawLine(xStart, yStart, xStart, yTop);
  103.     int j=((left <0)?xGap+left:left);
  104.     for(int i = j+xStart; i <= xTop; i += xGap) {
  105.         g.drawLine(i, yStart, i, yStart + 5);
  106.     }
  107.     for(int i = yStart; i >= yTop; i -= yGap) {
  108.         g.drawLine(xStart, i, xStart-5, i);
  109.     }
  110.     }
  111.     
  112.     // if the scrollbar has been used than repaint.
  113.     public boolean handleEvent(Event evt) {
  114.     switch(evt.id) {
  115.       case Event.SCROLL_ABSOLUTE:
  116.         repaint();
  117.         return true;
  118.       case Event.SCROLL_LINE_DOWN:
  119.         repaint();
  120.         return true;
  121.           case Event.SCROLL_LINE_UP:
  122.         repaint();
  123.         return true;
  124.       case Event.SCROLL_PAGE_UP:
  125.         repaint();
  126.         return true;
  127.       case Event.SCROLL_PAGE_DOWN:    
  128.         repaint();
  129.         return true;
  130.     }     
  131.     return false;
  132.     }
  133.     
  134.     // return the number of gaps that are in the moved
  135.     protected int getCaps(int moved) {
  136.         if(moved > xGap|| moved < (xGap* -1)) {
  137.         return moved / xGap;
  138.     }    
  139.     
  140.     return 0;
  141.     }        
  142.     
  143.     // return whats left between number of Gaps and moved 
  144.     protected int getLeft(int gaps, int moved) {
  145.         return (moved - (gaps * xGap));
  146.     } 
  147.     
  148.     // set the scrollbar to the desired values
  149.     public void setScrollbar(int value, int visible,int min, int max) {
  150.         scrollbar.setValues(value, visible, min, max);
  151.     }
  152.     
  153.     // return xGap
  154.     public int xGap() {
  155.         return xGap;
  156.     }
  157.     
  158.     // return yGap
  159.     public int yGap() {
  160.         return yGap;
  161.     }
  162.         
  163.     // return the xStart
  164.     public int xStart() {
  165.     return xStart;
  166.     }
  167.     
  168.     // return yStart
  169.     public int yStart() {
  170.     return size().height - 70;
  171.     }
  172.     
  173.     // return yTop
  174.     public int yTop() {
  175.         return 100;
  176.     }
  177.     
  178.     // return xTop
  179.     public int xTop() {
  180.     return size().width - 50;
  181.     }
  182. }
  183.  
  184.  
  185. /* Abstract class Diagram, all diagrams used by Display have as parrent 
  186.  * this class.
  187.  */
  188. abstract class Diagram {
  189.     
  190.     protected Display display; 
  191.     abstract void drawData(Graphics g,int Gaps, int left, int moved);
  192. }
  193.     
  194.  
  195.  
  196.  
  197.  
  198.  
  199.